package pos.Driving.Train;


import javax.media.j3d.*;
import javax.vecmath.*;
import pos.Driving.Path.PosPoint;
import pos.utils.ObjectFactory;
/**
 *
 * @author plus
 */
public class Bogie extends TransformGroup{
    private Train MyTrain;
    private Group VisualBogie;
    double offsetD;
    PosPoint currentPos=new PosPoint();
    
    public Bogie(Train MyTrain, double offsetD, String ModelFileKey) {
        this.MyTrain = MyTrain;
        this.currentPos.path_ID=MyTrain.getTrainPos(true).path_ID;
        this.offsetD = offsetD;
        VisualBogie = ObjectFactory.createObject(ModelFileKey);
        Transform3D trans = new Transform3D();
        trans.set(new Vector3d(offsetD, 0d, 0d));
        //this.setTransform(trans);
        this.setCapability(this.ALLOW_TRANSFORM_WRITE);
        this.addChild(VisualBogie);
        MyTrain.getTrainGroup().addChild(this);
//        RefreshPosition();
    }
    
    public PosPoint RefreshPosition(){
        currentPos.d=MyTrain.getTrainPos(true).d+offsetD;
        currentPos=MyTrain.refreshPosition(currentPos);
        setBogieTransform(currentPos);
        return currentPos;
    }
    
    private void setBogieTransform(PosPoint p){
        Transform3D transH=new Transform3D();
        transH.rotX(p.AngH);
        Transform3D transV=new Transform3D();
        transV.rotY(p.AngV);
        Transform3D transT=new Transform3D();
        transT.set(new Vector3d(p.pos.x,p.pos.y,p.pos.z));
        
        transT.mul(transV);
        transT.mul(transH);
        
        this.setTransform(transT);
    }
    
    public void setCurrentPath(int pathID){
        this.currentPos.path_ID=pathID;
    }
}
